package com.example.multiactivitydemo

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private lateinit var tvResult: TextView

    companion object {
        private const val REQUEST_CODE_SECOND = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tvResult = findViewById(R.id.tvResult)

        val btnFirst: Button = findViewById(R.id.btnFirst)
        val btnSecond: Button = findViewById(R.id.btnSecond)
        val btnThird: Button = findViewById(R.id.btnThird)
        val btnFourth: Button = findViewById(R.id.btnFourth)

        // 第一个按钮：正向传参
        btnFirst.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java).apply {
                putExtra("message", "Hello from MainActivity!")
                putExtra("number", 12345)
            }
            startActivity(intent)
        }

        // 第二个按钮：返回结果
        btnSecond.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_SECOND)
        }

        // 第三个按钮：拍照
        btnThird.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

        // 第四个按钮：秒表
        btnFourth.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_CODE_SECOND && resultCode == Activity.RESULT_OK) {
            data?.getStringExtra("return_data")?.let { returnedData ->
                tvResult.text = "返回的数据: $returnedData"
            }
        }
    }
}